$(function(){
	
})
var loginGetSingleOpenIdtype;
function layuiwindows(src){
	layui.use('layer', function(){ 
		layer.open({
			  type: 1,
			  closeBtn: 1, //不显示关闭按钮
			  skin: 'layui-layer-rim', //加上边框
			  anim: 2,
			  shadeClose: true, //开启遮罩关闭
			  content: $(''+src+''),
			  btn: '关闭',
			  btnAlign: 'c', //按钮居中
			  shade: 0 ,//不显示遮罩
			  area: ['300px', 'auto'],
			  yes: function(){
			          layer.closeAll();
			        }
			});
	})
}
/**
 * 获取验证码
 */
var count = 60;
function getcaptcha() {
	var mobile = $("#mobile").val();
	var json = {}
	var index = layer.load(2, {
		shade: [0.5, '#fff'],
		time: 3000
	});
	if(count==60){
		if(!mobile.match(/^1[34578]\d{9}$/)) {
			tippopup("#mobile","输入正确的手机号")
		}else{
			if(getCookie("roleId")=="9"||!getCookie("roleId")||getCookie("roleId")=="undefined"){
				
				
				var data= httpget(''+httpipstr+'alfa-mobile-ws/rest/user/getCaptchaForWorker/' + mobile + '');
				layer.closeAll();
				if (data.status == "failure") {
					if(data.message=="4"){
						 tippopup("#mobile","手机号不存在")
					}if(data.message=="3"){
						 tippopup("#mobile","角色非收运人员,不能获取手机验证码")
					}if(data.message=="1"){
						 tippopup("#mobile","角色收运人员,不能获取手机验证码")
					}
				} else {
					var countdown = setInterval(CountDown, 1000);
					function CountDown() {
						$('.send').css("background-color","#f1f1f1"); //将DIV标签隐藏。
						$('.send').css("color","#000000"); //将DIV标签隐藏。
						$('.send').html("重新发送(" + count + ")");
						if (count == 0) {
							$('.send').css("background-color","#FEC61B"); //将DIV标签隐藏。
							$('.send').css("color","#ffffff"); //将DIV标签隐藏。
							$('.send').html("发送验证码");
							clearInterval(countdown);
						}
						count--;
					}
				}
			}else{
				 tippopup("#mobile","您的账号已在产废登录。")
			}
			
			
		}
	}else{}
	
}

/**
 * 登录
 */
function login() {
	/*load()*/
	var index=layui.use('layer', function(){
		layer.load(2, {time: 3*1000})
	})
	var mobile = encode64($("#mobile").val());
	var captchanum = encode64($("#captchanum").val());
	var json = {
		"mobile": "" + mobile + "",
		"captcha": "" + captchanum + "",
		"openid":""+getCookie("openid")+"",
		"logintype":""+encode64("9")+""
	}
	if(getCookie("roleId")=="9"||!getCookie("roleId")||getCookie("roleId")=="undefined"){
		$.ajax({
			cache: false,
			type: "post",
			datatype: 'json',
			contentType: 'application/json;charset=UTF-8',
			url: ''+httpipstr+'alfa-mobile-ws/rest/user/loginforweixin',
			data: JSON.stringify(json),
			success: function(data) {
				if (data.message=="7") {
					if(data.data.roleId=="9"){
						setcookie("mobiletokensalesman", data.data.mobiletoken, 300, "/");
						setcookie("username", data.data.username, 300, "/");
						setcookie("salesmaniphone", data.data.phone, 300, "/");
							//触发事件
						location.href="../salesman/index1.jsp"
					}else{
						 tippopup("#mobile","您的角色不正确，无法登录")
					}

					
				} else {
					if(data.message=="1"){
						 tippopup("#mobile","服务器异常，请联系管理员。")
					} if(data.message=="2"){
						 tippopup("#captchanum","请输入手机号和验证码。")
					} if(data.message=="3"){
						tippopup("#captchanum","验证码不正确。")
					} if(data.message=="4"){
						tippopup("#captchanum","验证码已经超过有效时间。")
					} if(data.message=="5"){
						tippopup("#mobile","帐号不存在")
					}
					 if(data.message=="15"){
						 tippopup("#mobile","您的角色不正确，无法登录")
						}else if(data.message=="6"){
						tippopup("#mobile","openid")
					}
				}

			}
		});
	}else{
		 tippopup("#mobile","您的账号已在产废登录。")
	}

}
function exit(){
	clearCookieWithId("mobiletokenuser");
	location.href="../salesman/login.jsp"
}
function GetSingleOpenId(openid){
	var json = {"openid": ""+openid+""}
	$.ajax({
		cache: false,
		type: "post",
		dataType:"json",/*加上datatype*/
		data: JSON.stringify(json),
		contentType: 'application/json;charset=UTF-8',
		url: ''+httpipstr+'alfa-mobile-ws/rest/OpenId/GetSingleOpenId',
		success: function(data) {
			if(data.message=="1"){
				$("#heard").attr('src',data.data.headimgurl); 
				
				if(data.data.mobiletoken==""||data.data.mobiletoken==null){
				}else{
					if(data.data.roleId=="10"){
						clearCookieAll()
						setcookie("roleId", data.data.roleId, 30, "/");
						setcookie("mobiletokenuser", data.data.mobiletoken, 30, "/");
						setcookie("username", data.data.mobile, 300, "/");
						setcookie("useriphone", data.data.mobile, 300, "/");
						setcookie("realname", data.data.realname, 300, "/");
						setcookie("orgname", data.data.orgname, 300, "/");
						setcookie("userid", data.data.sysuserid, 300, "/");
						setcookie("address", data.data.address, 300, "/");
					}else{
						clearCookieAll()
						setcookie("salesmanid", data.data.sysuserid, 300, "/");
						setcookie("mobiletokensalesman", data.data.mobiletoken, 300, "/");
						setcookie("username", data.data.username, 300, "/");
						setcookie("salesmaniphone", data.data.mobile, 300, "/");
						setcookie("roleId", data.data.roleId, 30, "/")
					}
				}				
			}else{
				var requestpath=$("#requestpath").val();
				$.ajax({
					cache: false,
					type: "post",
					dataType:"json",/*加上datatype*/
					data: JSON.stringify(json),
					contentType: 'application/json;charset=UTF-8',
					url: 'recycle/addopenidinfo',
					success: function(data) {
						if(data.code=="0"){
						}
					}
				})
			}
			
		}
	});
}
function GetSingleOpenId1(openid){
	var json = {"openid": ""+openid+""}
	$.ajax({
		cache: false,
		type: "post",
		dataType:"json",/*加上datatype*/
		data: JSON.stringify(json),
		contentType: 'application/json;charset=UTF-8',
		url: ''+httpipstr+'alfa-mobile-ws/rest/OpenId/GetSingleOpenId',
		success: function(data) {
			if(data.message=="1"){
				$("#heard").attr('src',data.data.headimgurl); 
				if(data.data.mobiletoken==""||data.data.mobiletoken==null){
					setcookie("loginGetSingleOpenIdtype", 0, 30, "/");
					setcookie("mobiletokensalesman", "", 300, "/");
				}else{
					setcookie("loginGetSingleOpenIdtype", "", 30, "/");
					if(getCookie("mobiletokenuser")){
						setcookie("mobiletokensalesman", "", 30, "/");
					}else{
						setcookie("mobiletokensalesman", data.data.mobiletoken, 300, "/");
						setcookie("username", data.data.username, 300, "/");
						setcookie("salesmaniphone", data.data.mobile, 300, "/");
					}
					
					}
			}else{
				$.ajax({
					cache: false,
					type: "post",
					dataType:"json",/*加上datatype*/
					data: JSON.stringify(json),
					contentType: 'application/json;charset=UTF-8',
					url: '../recycle/addopenidinfo',
					success: function(data) {
						if(data.code=="0"){
							setcookie("mobiletokensalesman", "", 300, "/");
						}
					}
				})
			}
			
		}
	});
}
function layuiwindows(id){
	layui.use('layer', function(){ //独立版的layer无需执行这一句
	layer.open({
        type: 1
        ,content: $(''+id+'')
        ,btn: '关闭'
        ,btnAlign: 'c' //按钮居中
        ,shade: 0 //不显示遮罩
        ,yes: function(){
          layer.closeAll();
        }
      })
	})
}
function load(){
	layui.use('layer', function(){ //独立版的layer无需执行这一句
		layer.open({
	        type: 3
	      })
		})
}

function httpget(url){
	var json
	$.ajax({
		type: "get",
		url: url,
		dataType: "json",
		async :false,
		success: function(data) {
			json= data
		}
			
		});
	return json
}
function httppost(url,json){
	var returndata
	$.ajax({
		cache: false,
		type: "post",
		datatype: 'json',
		contentType: 'application/json;charset=UTF-8',
		url: url,
		data: JSON.stringify(json),
		success: function(data) {
			returndata= data
		}
	});
	return returndata
}
function judgeloginstate(){
	var returndata="";
	var json = {
			"userId": "1"
		}
	$.ajax({
		cache: false,
		type : "post",
		datatype : 'json',
		contentType : 'application/json;charset=UTF-8',
		data: JSON.stringify(json),
		url : ''+httpipstr+'alfa-mobile-ws/rest/user/GetCurrentUserInfoForWeiXin?mobiletoken='+getCookie("mobiletokenuser")+'',
		success : function(data) {
			returndata=true;
		}, 
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			returndata=false;
			}
	});
	return returndata
}